﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Zzj.Viewmodel;

namespace Zzj.Windows
{
    /// <summary>
    /// KeyBoardWindow.xaml 的交互逻辑
    /// </summary>
    public partial class KeyBoardWindow : Window
    {
        public InputViewModel viewModel;
        public int maxLength = 0;
        public List<String> words = new List<string>(0);
        private Action<int, String> callbackAction;
        public String flag = "";

        public KeyBoardWindow()
        {
            InitializeComponent();

            this.Loaded += Win_Load;

            viewModel = new InputViewModel();
            viewModel.index = 0;
            this.DataContext = viewModel;
        }

        public KeyBoardWindow(Action<int, String> callbackAction)
        {
            InitializeComponent();
            this.Loaded += Win_Load;

            viewModel = new InputViewModel();
            viewModel.index = 0;
            this.DataContext = viewModel;
            this.callbackAction = callbackAction;
        }

        private void Win_Load(object sender, RoutedEventArgs e)
        {
            UIElementCollection buttons03 = keyPanel03.Children;
            foreach (UIElement element in buttons03)
            {
                if (element is Button)
                {
                    Button keyButton = (element as Button);
                    keyButton.Click += KeyButton_Click_03;
                }
            }
        }

        public void showKeys(int idx)
        {
            keyPanel03.Visibility = Visibility.Visible;
        }

        private void hide_keywin(object sender, MouseButtonEventArgs e)
        {
            this.Hide();
        }

        private void KeyButton_Click_03(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            String val = (String)button.Content;
            if (val == "删除")
            {
                if (viewModel.carNoArray.Count > 0)
                {
                    viewModel.carNoArray.RemoveAt(viewModel.carNoArray.Count - 1);
                    if (callbackAction != null)
                    {
                        callbackAction.Invoke(viewModel.carNoArray.Count, "");
                    }
                }
            }
            else
            {
                if (viewModel.carNoArray.Count < maxLength)
                {
                    if (val.Equals("."))
                    {
                        if (viewModel.carNoArray.Count == 0)
                        {
                            return;
                        }
                        foreach (var tmpVal in viewModel.carNoArray)
                        {
                            if (tmpVal.Equals("."))
                            {
                                return;
                            }
                        }
                    }
                    viewModel.carNoArray.Add(val);
                    callbackAction.Invoke(viewModel.carNoArray.Count - 1, val);
                }
            }
        }

        public void getFlag(String Flag)
        {
            flag = Flag;
        }

        private void yincang(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }
    }
}